package com.lbd99.data.lbd.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lbd99.data.lbd.service.TOrderService;
import com.lbd99.data.lbd.service.TUserService;
import com.lbd99.data.lbd.vo.SysAccount;
import com.lbd99.data.lbd.vo.TUser;

@Controller
@RequestMapping("tOrder")
public class TOrderController {

	@Autowired
	private TOrderService tOrderService;
	
	@Autowired
	private TUserService tUserService;
	
	/**
     * 分页查询数据
     * @param page
     * @param rows
     * @return
     */
	 @RequestMapping(value = "/getTOrderList")
	 @ResponseBody
	 public Map<String, Object> getTOrderList(String phone,String order_no,Integer status,String startTime,String endTime,int page,int rows){
		 Map<String, Object> result = new HashMap<String, Object>();
		 //拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 map.put("phone", phone);
		 map.put("order_no", order_no);
		 map.put("status", status);
		 if(StringUtils.isNotBlank(startTime)){
			 map.put("startTime", startTime+" 00:00:00");
		 }
		 if(StringUtils.isNotBlank(endTime)){
			 map.put("endTime", endTime+" 23:59:59");
		 }
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tOrderService.selectListMap(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 } 
	 
	 
	 /**
	  * 买卖记录
	  * @param phone
	  * @param order_no
	  * @param status
	  * @param startTime
	  * @param endTime
	  * @param page
	  * @param rows
	  * @return
	  */
	 @RequestMapping(value = "/getBusinessList")
	 @ResponseBody
	 public Map<String, Object> getBusinessList(String phone,Integer status,String startTime,String endTime,int page,int rows,HttpSession httpSession){
		 SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	    	Map<String, Object> result = new HashMap<String, Object>();
	    	Map<String,Object> map=new HashMap<String,Object>();
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		map.put("rootUserId",user.getId()+"");
	        	}
	        }
		 //拼装map条件
		 map.put("phone", phone);
		 map.put("status", status);
		 if(StringUtils.isNotBlank(startTime)){
			 map.put("startTime", startTime+" 00:00:00");
		 }
		 if(StringUtils.isNotBlank(endTime)){
			 map.put("endTime", endTime+" 23:59:59");
		 }
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tOrderService.getBusinessList(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 } 
	 
	 /**
	  * 订单关系记录
	  * 订单号，卖出方状态，买入方状态，分页信息
	  */
	 @RequestMapping(value = "/getTOrderRelationList")
	 @ResponseBody
	 public Map<String, Object> getTOrderRelationList(String order_no,String status_out,String status_in,int page,int rows,HttpSession httpSession){
		Map<String, Object> result = new HashMap<String, Object>();
		//拼装map条件
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("order_no", StringUtils.trim(order_no));
		map.put("status_out", StringUtils.trim(status_out));
		map.put("status_in",StringUtils.trim(status_in));
		/*===================================================================================================*/
	     SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		//根据用户id查出该用户伞下的ID集合(包含自身)
	        		List<Integer> ids = tUserService.selectTreeIds(user.getId());
	        		if(ids!=null && ids.size()>0){
	        			map.put("ids",ids);
	        		}
	        	}
	        }
		 /*======================================================================================================*/
		PageHelper.startPage(page, rows);
		List<Map<String,Object>> list = tOrderService.selectOrderRelationListByMap(map);
		PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		result.put("total", pages.getTotal());
		result.put("rows", pages.getList());
		return result;
	 }
	 /**
	  * 订单关系记录(当天成交记录)
	  * 订单号，卖出方状态，买入方状态，分页信息
	  */
	 @RequestMapping(value = "/getTOrderRelationListToday")
	 @ResponseBody
	 public Map<String, Object> getTOrderRelationListToday(int page,int rows){
		 Map<String, Object> result = new HashMap<String, Object>();
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tOrderService.selectOrderRelationListToday();
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 }
	 
	 /**
	  * 订单关系记录(本周成交记录)
	  * 订单号，卖出方状态，买入方状态，分页信息
	  */
	 @RequestMapping(value = "/getTOrderRelationListWeek")
	 @ResponseBody
	 public Map<String, Object> getTOrderRelationListWeek(int page,int rows,HttpSession httpSession){
		//拼装map条件
		 Map<String,Object> map=new HashMap<String,Object>();
		 /*===================================================================================================*/
	     SysAccount sysAccount = (SysAccount)httpSession.getAttribute("SESSION_INFO_OBJECT");//获取当前登录的用户
	        if(sysAccount.getType()==1){//如果是普通管理员，用手机号查询出user信息
	        	TUser user = tUserService.selectByPhone(sysAccount.getTel());
	        	if(user!=null){
	        		//根据用户id查出该用户伞下的ID集合(包含自身)
	        		List<Integer> ids = tUserService.selectTreeIds(user.getId());
	        		if(ids!=null && ids.size()>0){
	        			map.put("ids",ids);
	        		}
	        	}
	        }
		 /*======================================================================================================*/
		 Map<String, Object> result = new HashMap<String, Object>();
		 PageHelper.startPage(page, rows);
		 List<Map<String,Object>> list = tOrderService.selectOrderRelationListWeek(map);
		 PageInfo<Map<String,Object>> pages = new PageInfo<Map<String,Object>>(list);
		 result.put("total", pages.getTotal());
		 result.put("rows", pages.getList());
		 return result;
	 }
	
	 @RequestMapping(value = "/handlerAppeal")
	 @ResponseBody
	 public Map<String, Object> handlerAppeal(Integer id,Integer status){
		 return tOrderService.handlerAppeal(id,status);
	 }
	 
	 
	 
	 
	 
	
}
